//
//  QCEmotionTools.swift
//  SwiftDemoWithSina
//
//  Created by qu on 2017/2/11.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit

//最大列数
let QCEMOTIONMAXCOL = 7
//最大行数
let QCEMOTIONMAXROW = 3

//每一页显示最多的表情个数20
let QCEMOTIONMAXCOUNT = QCEMOTIONMAXROW*QCEMOTIONMAXCOL - 1

class QCEmotionTools: NSObject {

  //全局访问点
    static let shared:QCEmotionTools = QCEmotionTools()
    
    let file = ((NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)).last! as NSString).appendingPathComponent("recent.archiver")
    lazy var emotionBundle:Bundle = {
       //路径
        let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)!
        //获取bundle
        let bundle = Bundle(path: path)!
        return bundle
    }()
    // 最近表情一堆数组 最多就是20个人
    lazy var recentEmotions:[QCEmotionModel] = {
        return self.getRecentEmotions()
    }()
    //默认表情
    lazy var normalEmotions:[QCEmotionModel] = {
        return self.loadEmotionArray(name:"default")
        
    }()
    //emoji
    lazy var emojiEmotions:[QCEmotionModel] = {
       return self.loadEmotionArray(name:"emoji")
        
    }()
    
    //浪小花
    lazy var lxhEmotions:[QCEmotionModel] = {
        return self.loadEmotionArray(name:"lxh")
    }()
    
    //三维数组
    lazy var allEmotions:[[[QCEmotionModel]]] = {
      
      return [
            [self.recentEmotions],
            
            self.loadEmotionGroupArray(emotions:self.normalEmotions),
            self.loadEmotionGroupArray(emotions:self.emojiEmotions),
            self.loadEmotionGroupArray(emotions:self.lxhEmotions)
        ]
      
    }()
}

//做最近表情的 保存和获取的操作

extension QCEmotionTools{
    //保存表情模型
    func saveRecentModel(emotionModel: QCEmotionModel){
        //遍历当前的最近表情的数组->去重
        for (i,model) in recentEmotions.enumerated() {
            //判断类型 是emoji 还是图片
            if model.isEmoji {
                
                if model.code == emotionModel.code {
                    recentEmotions.remove(at: i)
                }
            }else {
                if model.png == emotionModel.png {
                    recentEmotions.remove(at: i)
                }
            }
        }
        
        //添加到最近表情数组中
        recentEmotions.insert(emotionModel, at: 0)
        //判断超过20干掉最后一个
        if recentEmotions.count > 20 {
            recentEmotions.removeLast()
        }
        //将三维数组中的最近 表情数组进行更改
        allEmotions[0][0] = recentEmotions
        
        //保存到沙盒中
        NSKeyedArchiver.archiveRootObject(recentEmotions, toFile: file)
        
    }
        //从沙盒中获取 是否有没有最近表情的数组
        func getRecentEmotions()-> [QCEmotionModel]{
         
            //判断沙盒中是否保存最近表情
            if let emotions = NSKeyedUnarchiver.unarchiveObject(withFile: file) as? [QCEmotionModel] {
                
                return emotions
            }else {
                //第一次 没有保存 返回空数组
                return [QCEmotionModel]()
            }
        }
   
}

extension QCEmotionTools{
   //通过该方法分别获取不同的表情包的一维数组
    func loadEmotionArray(name:String)->[QCEmotionModel]{
        
        let file = emotionBundle.path(forResource: "\(name)/info.plist", ofType: nil)!
        
        let plistArray = NSArray(contentsOfFile: file)!
        
        let tempArray = NSArray.yy_modelArray(with: QCEmotionModel.self, json: plistArray) as! [QCEmotionModel]
        //遍历
        for model in tempArray{
            //给path赋值
          model.path = "\(name)" + "/" + "\(model.png ?? "")"
        }
        return tempArray
    }
    
    func loadEmotionGroupArray(emotions:[QCEmotionModel])->[[QCEmotionModel]]{
        
        //得到一维数组将要显示在表情键盘的页数
        let pageCount = (emotions.count + QCEMOTIONMAXCOUNT-1)/QCEMOTIONMAXCOUNT
        //创建一个二维数组
        var groupArray:[[QCEmotionModel]] = [[QCEmotionModel]]()
        
        for i in 0..<pageCount{
            //位置
            let loc = i * QCEMOTIONMAXCOUNT
            //长度
            var len = QCEMOTIONMAXCOUNT
            
            if len + loc > emotions.count {
                
                len = emotions.count - loc
            }
            //获取范围
            let range = NSRange(location: loc, length: len)
            //截取每个数组
            let tempArry = (emotions as NSArray).subarray(with: range) as! [QCEmotionModel]
            groupArray.append(tempArry)
            
        }
        
        return groupArray
    }
    
}
